package com.ywk.pattern._21_singleton;

import com.ywk.pattern._21_singleton._21_9_enum.Singleton;

/**
 * @description:
 * @author: ${ywk}
 * @date: 2020-06-07
 */
class User{
    private User() {

    }
    static enum SingletonEnum{
        INSTANCE;

        private User user;

        private SingletonEnum() {
            user = new User();
        }

        public User getInstance() {
            return user;
        }
    }

    public static User getInstance() {
        return SingletonEnum.INSTANCE.getInstance();
    }
}

public class _21_10_enum_user {
    static User singleton = null;
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    User instance = User.getInstance();
                    if (singleton == null) {
                        singleton = instance;
                    }
                    System.out.println(singleton == instance);

                }
            }).start();
        }
    }


}
